package com.tupu.service.impl.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.admin.LabelInfo;
import com.tupu.entity.admin.LabelVisualized;
import com.tupu.entity.admin.RelationBuildingInfo;
import com.tupu.entity.admin.RelationInfo;
import com.tupu.mapper.admin.KnowledgeListMapper;
import com.tupu.mapper.admin.LabelMapper;
import com.tupu.mapper.admin.RelationBuildingMapper;
import com.tupu.mapper.admin.RelationMapper;
import com.tupu.service.admin.RelationBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class RelationBuildingServiceImpl extends ServiceImpl<RelationBuildingMapper, RelationBuildingInfo> implements RelationBuildingService {

    @Autowired(required = false)
    private RelationBuildingMapper relationBuildingMapper;

    @Autowired(required = false)
    private RelationMapper relationMapper;

    @Autowired(required = false)
    private LabelMapper labelMapper;

    @Autowired(required = false)
    private KnowledgeListMapper knowledgeListMapper;

    @Override
    public List<RelationBuildingInfo> page(List<Long> ids) {
        List<RelationBuildingInfo> list = new ArrayList<>();
        for (Long id : ids) {
            QueryWrapper<RelationBuildingInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("relation_id", id);
            queryWrapper.orderByAsc("id");
            List<RelationBuildingInfo> records = relationBuildingMapper.selectList(queryWrapper);
            for (RelationBuildingInfo rb : records) {
                rb.setStartName(labelMapper.findlabelName(rb.getStartId()));
                rb.setEndName(labelMapper.findlabelName(rb.getEndId()));
                rb.setRelationName(relationMapper.findRelationName(rb.getRelationId()));
                String listName = knowledgeListMapper.findName(rb.getNodeId());
                rb.setKnowledgeName(listName);
                list.add(rb);
            }
        }
        return list;
    }

    @Override
    public LabelVisualized relationSearch(Long node_id, String relation) {
        LabelVisualized Vis = new LabelVisualized();
        List<Map<String, Object>> labelList = new ArrayList<>();
        List<Map<String, Object>> linkList = new ArrayList<>();
        List<Map<String, Object>> tagsList = new ArrayList<>();
        Vis.setRootId(node_id);
        Vis.setTags(tagsList);

        QueryWrapper<RelationInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("node_id",node_id);
        wrapper.eq("name",relation);
        RelationInfo one = relationMapper.selectOne(wrapper);

        QueryWrapper<RelationBuildingInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("relation_id", one.getId());
        queryWrapper.eq("node_id", node_id);
        List<RelationBuildingInfo> lists = relationBuildingMapper.selectList(queryWrapper);
        for (RelationBuildingInfo relationInfo : lists) {
            Long startId = relationInfo.getStartId();
            Long endId = relationInfo.getEndId();
            Map<String, Object> linkMap = new HashMap<>();
            linkMap.put("from", startId.toString());
            linkMap.put("to", endId.toString());
            linkMap.put("text", relation);
            linkList.add(linkMap);
            Vis.setLinks(linkList);
            //起始标签
            LabelInfo startLabel = labelMapper.selectById(startId);
            Map<String, Object> startLabelMap = new HashMap<>();
            startLabelMap.put("id", startId.toString());
            startLabelMap.put("color", startLabel.getColor());
            startLabelMap.put("borderWidth", "black");
            startLabelMap.put("fontColor", "0");
            startLabelMap.put("text", startLabel.getLabel());
            Map<String, Object> map = new HashMap<>();
            map.put("tag", "");
            startLabelMap.put("data", map);
            //到达标签
            LabelInfo endLabel = labelMapper.selectById(endId);
            Map<String, Object> endLabelMap = new HashMap<>();
            endLabelMap.put("id", endId.toString());
            endLabelMap.put("color", endLabel.getColor());
            endLabelMap.put("borderWidth", "black");
            endLabelMap.put("fontColor", "0");
            endLabelMap.put("text", endLabel.getLabel());
            endLabelMap.put("data", map);

            labelList.add(startLabelMap);
            labelList.add(endLabelMap);
            //去重
            Vis.setNodes(labelList.stream().distinct().collect(Collectors.toList()));
        }
        return Vis;
    }
}
